-- global_damage
-- create by panyl
-- 技能：全体伤害。对地牢中所有怪物造成伤害（分已显示和所有怪物两种）
-- 目前支持如下配置：
-- 1、global_damage(1200)：对目标造成1200威力的魔法伤害

return {
    apply = function(source, target, skillId, round, para, extra)
        local force = para[1];

        local combine = PropM.combine;
        local PropM_apply = PropM.apply;
        local fetchProps = PropM.fetchProps;
        local trigger = PropM.trigger;
        local prop;

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);


        --最终威力
        local force = math.modf(force * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 3. 伤害
        local damage = math.modf(force * (magic + 3) / 1000);

        -- 4. 遍历所有的怪
        -- 总伤害
        local totalDamage = 0;
        for p = 1, GRID_SIZE do
            local grid = DungeonM.getGridByPos(p);
            local monster = grid.monster;

            repeat

                -- 是未死亡的怪物
                if  not grid:isOpened() or
                    not monster or
                    grid.monster:isDead() or
                    (grid.monster:isBoss() and skillId == 182) then    -- 特殊技能对BOSS无效
                    break;
                end

                local finalDamage = damage;
                if #para > 1 and tonumber(para[2]) == monster.style then
                    -- 系别加成或削减
                    finalDamage = math.modf(para[3] * damage / 1000);

                    -- 无视系别免疫
                    prop = combine(source, "ignore_type", skillId);
                    if PropM_apply(prop, 1) > 0 and finalDamage == 0 then
                        finalDamage = damage;
                    end

                    -- 播放免疫效果  -- (CombatM.receiveDamage中有魔法免疫动画处理。这里不再添加)
                end

                -- 魔法暴击类，多种暴击线性叠加伤害
                local addon_damage = 0;

                -- 伤害类魔法概率增强
                local props = fetchProps(source, "damage_magic");
                for _, prop in ipairs(props) do
                    addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
                end
                -- 魔法暴击
                props = fetchProps(source, "critical_magic");
                for _, prop in ipairs(props) do
                    addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
                end
                -- 概率双倍
                props = fetchProps(source, "skill_double");
                for _, prop in ipairs(props) do
                    if tonumber(prop[2]) == skillId then
                        addon_damage = addon_damage + trigger(source, prop[1], prop[2], finalDamage);
                    end
                end
                -- 最终伤害
                finalDamage = finalDamage + addon_damage;

                prop = combine(monster, "resist", "magic");
                -- 忽视抗性
                local p = combine(source, "ignore_resist", skillId);
                prop[3] = PropM_apply(p, prop[3]);
                p = combine(source, "ignore_resist", "magic");
                prop[3] = PropM_apply(p, prop[3]);

                finalDamage = PropM_apply(prop, finalDamage);

                -- 增加命中动作
                if SkillM.getSequence(monster) == nil then
                    SkillM.getSequenceList()[monster] = CastSequence.create(source, monster, skillId);
                end

                SkillM.getSequence(monster):hit(source, monster, skillId);

                -- 受创
                CombatM.receiveDamage(source, monster, finalDamage, skillId, extra);

                totalDamage = totalDamage + finalDamage;

            until true;
        end

        -- 如果是玩家，抛出攻击结果事件
        if source.type == OBJECT_TYPE_USER then
            EventMgr.fire(event.HERO_ATTACK_RESULT, { ["damage"] = totalDamage, ["extra"] = extra});
        end

        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        if (#arr == 1) then
            return { tonumber(arr[1]), };
        else
            return { tonumber(arr[1]), arr[2], tonumber(arr[3]) };
        end
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local base = para[1];

        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);

        --最终威力
        local force = math.modf(base * (addon_rate + 1000)/1000);

        -- 2. 攻击方的魔力
        local magic = source:getMagic();

        -- 3. 伤害/计算时魔力+3
        local damageBase = math.modf(base * (magic + 3) / 1000);
        local damageForce = math.modf(force * (magic + 3) / 1000);
        local damageAddon = damageForce - damageBase;
        if damageAddon == 0 then
            desc = string.gsub(desc, "{global_damage}", damageForce);
        else
            desc = string.gsub(desc, "{global_damage}", damageBase .. "(+" .. damageAddon .. ")");
        end

        return desc;
    end,
};
